package simulator;

import java.util.HashSet;
import java.util.Set;

public class Pc {

	static double intervalOfTime = 1;
	private String name;
	private Set<Pc> connectedPcs;
	private State state;
	private double stateTimeLeft;
	
	public Pc(String name) {
		this.name = name;
		this.connectedPcs = new HashSet<Pc>();
		this.state = State.Sane;
		this.stateTimeLeft = 0;
	}
	
	public void addConection(Pc pc) {
		if(!this.connectedPcs.contains(pc)) {
			this.connectedPcs.add(pc);
			if(!pc.isConnectedTo(this)) {
				pc.addConection(this);
			}
		}
	}
	
	public void delConection(Pc pc) {
		if(this.connectedPcs.contains(pc)) {
			this.connectedPcs.remove(pc);
			if(pc.isConnectedTo(this)) {
				pc.delConection(this);
			}
		}
	}
	
	public boolean isConnectedTo(Pc pc) {
		return this.connectedPcs.contains(pc);
	}

	public String getName() {
		return name;
	}
	
	public int getConnectedQuantity() {
		return this.connectedPcs.size();
	}
	
	public int getConnectedInfectedQuantity() {
		int i=0;
		for(Pc pc: this.connectedPcs){
			if(pc.getState().equals(State.Infected)){
				i++;
			}
		}
		return i;
	}
	
	public Set<Pc> getConnectedPcs() {
		return this.connectedPcs;
	}
	
	public State getState() {
		return state;
	}
	
	public void setState(State state) {
		this.state = state;
	}
	
	public double getStateTimeLeft() {
		return stateTimeLeft;
	}

	public void setStateTimeLeft(double stateTimeLeft) {
		this.stateTimeLeft = stateTimeLeft;
	}
	
	public void decrementStateTimeLeft() {
		this.stateTimeLeft -= intervalOfTime;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Pc other = (Pc) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	public String getInfo() {
		StringBuilder info = new StringBuilder();
		info.append("Name: " + this.name + "\n");
		info.append("Connected to: ");
		for(Pc pc : this.connectedPcs) {
			info.append(pc.getName() + ", ");
		}
		return info.toString();
	}
	
}
